/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.uha.ensisa.kfetmanager.category;

import fr.uha.ensisa.kfetmanager.customer.Customer;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.validation.ValidationException;

/**
 *
 * @author JC
 */
@Stateless
public class CustomerCategoryManagerBean implements CustomerCategoryManagerRemote {
    @PersistenceContext(unitName="kfet-manager-ejbPU")
    private EntityManager em;


    @Override
    public CustomerCategory createCategory(CustomerCategory category, String label) {
        if (category == null) throw new ValidationException("L'objet category est null");
        if (label == null || label.equals("")) throw new ValidationException("L'objet label est null ou vide");

        if(this.exists(label)) throw new ValidationException("Une catégorie ayant ce label existe déjà");

        category.setLabel(label);
        category.setCustomers(new LinkedList<Customer>());

        em.persist(category);
        return category;
    }

    @Override
    public CustomerCategory updateCategory(CustomerCategory category, String label) {
        if (category == null) throw new ValidationException("L'objet category est null");
        if (label == null || label.equals("")) throw new ValidationException("L'objet label est null ou vide");

        if(!label.equals(category.getLabel()) && this.exists(label)) throw new ValidationException("Une catégorie ayant ce label existe déjà");

        category.setLabel(label);

        em.merge(category);
        return category;
    }

    @Override
    public CustomerCategory getCategory(Long id) {
        if (id == null) throw new ValidationException("ID est null");
        try {
            return (CustomerCategory)em.find(CustomerCategory.class, id);
        } catch(Exception e) {
            return null;
        }
    }

    @Override
    public List<CustomerCategory> getCategories() {
        Query query = em.createQuery("SELECT c FROM CustomerCategory c");
        List<CustomerCategory> categories = query.getResultList();

        return categories;
    }

    @Override
    public Iterator<CustomerCategory> iterator() {
        return this.getCategories().iterator();
    }

    @Override
    public void deleteCategory(CustomerCategory category) {
        if (category == null) throw new ValidationException("L'objet category est null");

//        if (category.getCustomers().size() != 0)
        em.remove(em.merge(category));
    }

    @Override
    public boolean exists(String label) {
        Query query = em.createQuery("SELECT c FROM CustomerCategory c WHERE lower(c.label) LIKE :lbl")
                .setParameter("lbl", label.toLowerCase());
        List<CustomerCategory> cats = query.getResultList();

        if (cats.size() != 0) return true;

        return false;
    }


}
